//
//  AppDelegate.swift
//  微博
//
//  Created by YJW on 16/8/4.
//  Copyright © 2016年 yijiang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white()
        window?.rootViewController = YJMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadData()
        
        return true
    }
}

extension AppDelegate {
    
    private func loadData(){
        
        DispatchQueue.global().async { 
            
            //1.url
            let url = Bundle.main().urlForResource("weibo.json", withExtension: nil)
            
            //2.data
            let data = NSData(contentsOf: url!)
            
            //3.文件路径
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let path = (docDir as NSString).appendingPathComponent("weibo.json")
            
            //4.将 data 写入文件，直接保存在沙盒，等待应用程序下一次启动时调用
            data?.write(toFile: path, atomically: true)
            print("jiazai --- \(path)")
        }
    }
}
